package com.biosan.DaoTnterface;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.dbcp.BasicDataSource;

import com.biosan.utils.BiosanUtils;

public class Copy_life_test {

	public static void main(String[] args) {
		
		//SQL杩炴帴
	   // BiosanUtils.executeUpdate(sql, params)
		int i=0;
		Date nowDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal=Calendar.getInstance(); 
		cal.add(Calendar.DATE,-1);
		System.out.println(sdf.format(cal.getTime()));

		StringBuffer _importDataBuffer = new StringBuffer();
		try {
			String sql;
			Connection conn;
			Statement stmt;
			ResultSet rs;
		
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
					.newInstance();
			/*
			 * 
			String url = "jdbc:microsoft:sqlserver://192.168.16.254:1433;DatabaseName=wcdbsz";
			String url = "jdbc:microsoft:sqlserver://172.25.220.25:1433;DatabaseName=wcdbsz";
			String user = "xiaoyu";
			String password = "xiaoyu2010";
			*/
			
			String url = "jdbc:microsoft:sqlserver://192.168.16.254:1433;DatabaseName=yesc2";
			String user = "sa";
			String password = "biosan";
			String samplestring = "";
			String cycle = "";
//			String[] array =null;
			String cycle_l = "";
			String cycle_d = "";
			String lmpdate = "";
			String birthday = "";
			String collectdate = "";
			String str = "";
			conn = DriverManager.getConnection(url, user, password);
			stmt = conn.createStatement();
			sql = "select * from lh";
			System.out.println(sql);
		//	sql = "select * from v_szxsesc ";
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String dep = rs.getString("A15");
					_importDataBuffer.append("<Specimen>");
					samplestring = rs.getString("A1");
					_importDataBuffer.append("<SampleID>");
					_importDataBuffer.append(samplestring);
					_importDataBuffer.append("</SampleID>");
	
					_importDataBuffer.append("<PatientID>");
					_importDataBuffer.append(samplestring+"2011");
					_importDataBuffer.append("</PatientID>");
	
					_importDataBuffer.append("<LastName>");
					_importDataBuffer.append(rs.getString("A1"));
					_importDataBuffer.append("</LastName>");
	
					_importDataBuffer.append("<FirstName>");
					_importDataBuffer.append("</FirstName>");
	
					birthday = rs.getString("A11");
					System.out.println("111==="+birthday);
					_importDataBuffer.append("<BirthDate>");
					_importDataBuffer.append(birthday.substring(0,10).replace("-", ""));
					_importDataBuffer.append("</BirthDate>");
	
					_importDataBuffer.append("<Ethnicity>");
					_importDataBuffer.append("0");
					_importDataBuffer.append("</Ethnicity>");
	
					_importDataBuffer.append("<NoOfFetuses>");
					_importDataBuffer.append(rs.getString("A13").replace(".0", ""));
					_importDataBuffer.append("</NoOfFetuses>");
					
					lmpdate = rs.getString("A14")==null?"":rs.getString("A14");
					_importDataBuffer.append("<LMPDates>");
					if(!(lmpdate.equals("") || lmpdate=="" || lmpdate=="null"))
					_importDataBuffer.append(lmpdate.substring(0,10).replace("-", ""));
					_importDataBuffer.append("</LMPDates>");
	
					_importDataBuffer.append("<Smoking>");
					_importDataBuffer.append("0");
					_importDataBuffer.append("</Smoking>");
	
					_importDataBuffer.append("<Weight>");
					_importDataBuffer.append(rs.getString("A5"));
					_importDataBuffer.append("</Weight>");
	
					_importDataBuffer.append("<Insulin>");
					_importDataBuffer.append("0");
					_importDataBuffer.append("</Insulin>");
					
					collectdate = rs.getString("A2");
					_importDataBuffer.append("<CollectionDate>");
					_importDataBuffer.append(rs.getString("A2").substring(0,10).replace("-", ""));
					_importDataBuffer.append("</CollectionDate>");
	
					_importDataBuffer.append("<ReceivedDate>");
					_importDataBuffer.append("</ReceivedDate>");
					
					str = rs.getString("A6");
					
					_importDataBuffer.append("<ScanDates>");
					if(str.equals("LMP"))
					{
						if(!(lmpdate.equals("")||lmpdate==""))
						_importDataBuffer.append(lmpdate.substring(0,10).replace("-", ""));//b超时间--LMP时间
					}
					else if(str.equals("B超"))
					{	
						if(!(collectdate.equals("")||collectdate==""))
						_importDataBuffer.append(collectdate.substring(0,10).replace("-", ""));//b超时间--采样时间
					}
					_importDataBuffer.append("</ScanDates>");
	
					_importDataBuffer.append("<CRL>");
	//				_importDataBuffer.append(rs.getString("A65"));
					_importDataBuffer.append("</CRL>");
	
					_importDataBuffer.append("<CRL_twin>");
	//				_importDataBuffer.append(_CRL_twin);
					_importDataBuffer.append("</CRL_twin>");
	
					_importDataBuffer.append("<BPD>");
	//				_importDataBuffer.append(_BPD);
					_importDataBuffer.append("</BPD>");
	
					_importDataBuffer.append("<HC>");
	//				_importDataBuffer.append(_HC);
					_importDataBuffer.append("</HC>");
					
					
					cycle = rs.getString("A4");
					if(cycle.indexOf(".")==-1){
						cycle_l = cycle;
						cycle_d = "0";
					}
					else{
					cycle_l = cycle.substring(0, cycle.indexOf("."));
					cycle_d = cycle.substring(cycle.indexOf(".")+1, cycle.indexOf(".")+2);
					}
	//				System.out.println("cycle="+cycle_l+cycle_d);
					_importDataBuffer.append("<ManualEntry>");
	//				System.out.println(cycle+"====="+cycle_l);
					_importDataBuffer.append((Integer.parseInt(cycle_l)*7+Integer.parseInt(cycle_d)));
					_importDataBuffer.append("</ManualEntry>");
	
					_importDataBuffer.append("<NT>");
//					_importDataBuffer.append(rs.getString("A31"));
					_importDataBuffer.append("</NT>");
	
					_importDataBuffer.append("<NT_twin>");
	//				_importDataBuffer.append(_NT_twin);
					_importDataBuffer.append("</NT_twin>");
	
					_importDataBuffer.append("<NB>");
	//				_importDataBuffer.append(_NB);
					_importDataBuffer.append("</NB>");
	
					_importDataBuffer.append("<NB_twin>");
	//				_importDataBuffer.append(_NB_twin);
					_importDataBuffer.append("</NB_twin>");
	
					_importDataBuffer.append("<Method>");
	//				_importDataBuffer.append(_Method);
					_importDataBuffer.append("</Method>");
	
					_importDataBuffer.append("<TransferDate>");
	//				_importDataBuffer.append(_TransferDate);
					_importDataBuffer.append("</TransferDate>");
	
					_importDataBuffer.append("<ExtractionDate>");
	//				_importDataBuffer.append(_ExtractionDate);
					_importDataBuffer.append("</ExtractionDate>");
	
					_importDataBuffer.append("<AgeAtExtraction>");
	//				_importDataBuffer.append(_AgeAtExtraction);
					_importDataBuffer.append("</AgeAtExtraction>");
	
					_importDataBuffer.append("<DonorBirthDate>");
	//				_importDataBuffer.append(_DonorBirthDate);
					_importDataBuffer.append("</DonorBirthDate>");
					
					
					_importDataBuffer.append("<CalcBasedOn>");
					if(str.equals("LMP"))
					_importDataBuffer.append("0");
					else if(str.equals("B超"))
					_importDataBuffer.append("1");
					else
					_importDataBuffer.append("0");
					_importDataBuffer.append("</CalcBasedOn>");
	
					_importDataBuffer.append("<AFP>");
					_importDataBuffer.append(rs.getString("A25"));
					_importDataBuffer.append("</AFP>");
	
					_importDataBuffer.append("<hCGbM>");
					_importDataBuffer.append(rs.getString("A26"));
					_importDataBuffer.append("</hCGbM>");
	
					_importDataBuffer.append("<Ue3>");
					_importDataBuffer.append(rs.getString("A27"));
					_importDataBuffer.append("</Ue3>");
	
					_importDataBuffer.append("<PAPPA>");
//					_importDataBuffer.append(rs.getString("A29"));
					_importDataBuffer.append("</PAPPA>");
	
					_importDataBuffer.append("<hCGb>");
//					_importDataBuffer.append(rs.getString("A28"));
					_importDataBuffer.append("</hCGb>");
	
					_importDataBuffer.append("</Specimen>");
			
//				_importDataBuffer.append("<Specimen>");
//
//				_importDataBuffer.append("<SampleID>");
//				_importDataBuffer.append(rs.getString("A32"));
//				_importDataBuffer.append("</SampleID>");
//
//				_importDataBuffer.append("<PatientID>");
//				_importDataBuffer.append(samplestring.substring(2, samplestring.length()));
//				_importDataBuffer.append("</PatientID>");
//
//				_importDataBuffer.append("<LastName>");
//				_importDataBuffer.append(rs.getString("A1"));
//				_importDataBuffer.append("</LastName>");
//
//				_importDataBuffer.append("<FirstName>");
////				_importDataBuffer.append(rs.getString(""));
//				_importDataBuffer.append("</FirstName>");
//
//				_importDataBuffer.append("<BirthDate>");
//				_importDataBuffer.append(rs.getString("A34").substring(0,10).replace("-", ""));
//				_importDataBuffer.append("</BirthDate>");
//
//				_importDataBuffer.append("<Ethnicity>");
//				_importDataBuffer.append("0");
//				_importDataBuffer.append("</Ethnicity>");
//
//				_importDataBuffer.append("<NoOfFetuses>");
//				_importDataBuffer.append(rs.getString("A41"));
//				_importDataBuffer.append("</NoOfFetuses>");
//
//				lmpdate = rs.getString("A36");
//				_importDataBuffer.append("<LMPDates>");
//				_importDataBuffer.append(rs.getString("A36").substring(0,10).replace("-", ""));
//				_importDataBuffer.append("</LMPDates>");
//
//				_importDataBuffer.append("<Smoking>");
//				_importDataBuffer.append("0");
//				_importDataBuffer.append("</Smoking>");
//
//				_importDataBuffer.append("<Weight>");
//				_importDataBuffer.append(rs.getString("A40"));
//				_importDataBuffer.append("</Weight>");
//
//				_importDataBuffer.append("<Insulin>");
//				_importDataBuffer.append("0");
//				_importDataBuffer.append("</Insulin>");
//				
//				collectdate = rs.getString("A37");
//				_importDataBuffer.append("<CollectionDate>");
//				_importDataBuffer.append(rs.getString("A37").substring(0,10).replace("-", ""));
//				_importDataBuffer.append("</CollectionDate>");
//
//				_importDataBuffer.append("<ReceivedDate>");
////				_importDataBuffer.append(e_ReceivedDate);
//				_importDataBuffer.append("</ReceivedDate>");
//
//				_importDataBuffer.append("<ScanDates>");
//				_importDataBuffer.append(rs.getString("A36").substring(0,10).replace("-", ""));
//				_importDataBuffer.append("</ScanDates>");
//
//				_importDataBuffer.append("<CRL>");
////				_importDataBuffer.append(e_CRL);
//				_importDataBuffer.append("</CRL>");
//
//				_importDataBuffer.append("<CRL_twin>");
////				_importDataBuffer.append(e_CRL_twin);
//				_importDataBuffer.append("</CRL_twin>");
//
//				_importDataBuffer.append("<BPD>");
////				_importDataBuffer.append(e_BPD);
//				_importDataBuffer.append("</BPD>");
//
//				_importDataBuffer.append("<HC>");
////				_importDataBuffer.append(e_HC);
//				_importDataBuffer.append("</HC>");
//				
//				cycle = rs.getString("A39");
//				cycle_l = cycle.substring(0, cycle.indexOf("."));
//				cycle_d = cycle.substring(cycle.indexOf(".")+1, cycle.length());
//				_importDataBuffer.append("<ManualEntry>");
//				_importDataBuffer.append((Integer.parseInt(cycle_l)*7+Integer.parseInt(cycle_d)));
//				_importDataBuffer.append("</ManualEntry>");
//
//				_importDataBuffer.append("<NT>");
////				_importDataBuffer.append(e_NT);
//				_importDataBuffer.append("</NT>");
//
//				_importDataBuffer.append("<NT_twin>");
////				_importDataBuffer.append(e_NT_twin);
//				_importDataBuffer.append("</NT_twin>");
//
//				_importDataBuffer.append("<NB>");
////				_importDataBuffer.append(e_NB);
//				_importDataBuffer.append("</NB>");
//
//				_importDataBuffer.append("<NB_twin>");
////				_importDataBuffer.append(e_NB_twin);
//				_importDataBuffer.append("</NB_twin>");
//
//				_importDataBuffer.append("<Method>");
////				_importDataBuffer.append(e_Method);
//				_importDataBuffer.append("</Method>");
//
//				_importDataBuffer.append("<TransferDate>");
////				_importDataBuffer.append(e_TransferDate);
//				_importDataBuffer.append("</TransferDate>");
//
//				_importDataBuffer.append("<ExtractionDate>");
////				_importDataBuffer.append(e_ExtractionDate);
//				_importDataBuffer.append("</ExtractionDate>");
//
//				_importDataBuffer.append("<AgeAtExtraction>");
////				_importDataBuffer.append(e_AgeAtExtraction);
//				_importDataBuffer.append("</AgeAtExtraction>");
//
//				_importDataBuffer.append("<DonorBirthDate>");
////				_importDataBuffer.append(e_DonorBirthDate);
//				_importDataBuffer.append("</DonorBirthDate>");
//
//				_importDataBuffer.append("<CalcBasedOn>");
//				_importDataBuffer.append("0");
//				_importDataBuffer.append("</CalcBasedOn>");
//
//				_importDataBuffer.append("<AFP>");
//				_importDataBuffer.append(rs.getString("A46"));
//				_importDataBuffer.append("</AFP>");
//
//				_importDataBuffer.append("<hCGbM>");
//				_importDataBuffer.append(rs.getString("A47"));
//				_importDataBuffer.append("</hCGbM>");
//
//				_importDataBuffer.append("<Ue3>");
////				_importDataBuffer.append(rs.getString(""));
//				_importDataBuffer.append("</Ue3>");
//
//				_importDataBuffer.append("<PAPPA>");
////				_importDataBuffer.append(e_PAPPA);
//				_importDataBuffer.append("</PAPPA>");
//
//				_importDataBuffer.append("<hCGb>");
////				_importDataBuffer.append(e_hCGb);
//				_importDataBuffer.append("</hCGb>");
//
//				_importDataBuffer.append("</Specimen>");
			
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String dataXml = "<?xml version=\"1.0\" standalone=\"yes\"?>";

		dataXml += "<NewDataSet>";
		dataXml += _importDataBuffer.toString();
		dataXml += "</NewDataSet>";
		
		 try {
             File f = new File("E:/zao_data.xml");
                if (f.exists()) {
                 f.delete();
            }
            f.createNewFile();

//            Date date = new Date();
//            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd-hhmm");
//           String s = sf.format(date).toString();
           FileOutputStream fos = new FileOutputStream(f);
           PrintWriter pw = new PrintWriter(fos);
//           System.out.print(_importDataBuffer.toString());

               pw.write(dataXml);
               pw.flush();
           fos.close();
           
           pw.close();


        } catch (Exception e) {

        }
	
}
	public static String getTrueDate1(String str){
		String date = "";
		String y = "";
		String m = "";
		String d = "";
		y = str.substring(str.indexOf("/",2)+1, str.indexOf("/",2)+5);
		d = str.substring(str.indexOf("/", 1)+1,str.indexOf("/", 2));
		m = str.substring(0,str.indexOf("/",1));
		if(m.length()<2)m="0"+m;
		if(d.length()<2)d="0"+d;
		date = y+m+d;
		return date;
	}
}